<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>主页面</title>
		<script src="../../../dist/xsloader.js" type="text/javascript" charset="utf-8"></script>
	</head>

	<body>
		<p>
			这是父页面：
			<div id="logDiv">

			</div>
			<iframe id="iframe" src="sub.html" style="width:100%;height:300px"></iframe>
		</p>
		<script type="text/javascript">
			window.divLog = function(obj) {
				var div = document.getElementById("logDiv");
				var txt = (typeof obj == "string") && obj || xsJson2String(obj) || "";
				txt = txt.replace(/\\r\\n|\\n/g, "\n");
				txt = txt.replace(/\\t/g, "    ");

				div.innerText = div.innerText + "\n" + txt;
			};
			xsloader({});
			require(["xsmsg"], function(xsmsg) {
				xsmsg.listenMessage({
					conndata: "from main",
					connectingSource: function(source, origin, conndata, callback) {
						xsloader.asyncCall(function() {
							if(conndata == "sub-main") {
								callback(true);
							}
						});
					},
					connected: function(sender, data) {
						divLog(data);
						sender({
							text: "main send:"+xsJson2String(data)
						});
					},
					listener: function(data) {
						divLog(data);
					}
				});
				xsmsg.listenMessage({
					conndata: "from main2",
					connectingSource: function(source, origin, conndata, callback) {
						xsloader.asyncCall(function() {
							if(conndata == "sub-main2") {
								callback(true);
							}
						});
					},
					connected: function(sender, data) {
						divLog(data);
						sender({
							text: "main2 send:"+xsJson2String(data)
						});
					},
					listener: function(data) {
						divLog(data);
					}
				});
			});
		</script>
	</body>

</html>